#!/usr/bin/python
# -*- coding: UTF-8 -*-
from itertools import islice

from mssql_tools import MSSQL


def format_num(n):
    if n<10:
        return "0"+str(n)
    return str(n)

fn="D:/byd_shell/Report_20190413"
rp="[DEBUG](SMDDatabase::NotifyReportStatus)"
ms = MSSQL(host="192.168.4.241", user="sa", pwd="jy@88250860", db="ESSMC_SZDX_PROC")
ms.createConnect()
row=0
start_row=1040000;
with open(fn, "r", encoding='gbk') as f:
    count = 0
    while 1:
        # 一次读取1M
        size=1024*1024
        lines =f.readlines(size)
        if not lines:
            break
        # print(len(lines))
        for line in lines:
            row = row + 1
            #从第几行开始执行
            if(row<start_row):continue
            #去掉后面的回车和换行
            line=line.strip()
            #替换前面打印，保留存储过程
            line=line.replace(rp,"exec")
            count=count+1
            print("row:"+str(row)+" sql:"+line)
            ms.executeBatch(line)#执行存储过程，不提交
            if(count>=500):
                count=0
                ms.commit()#每500个提交一次
                print("commit")
    if count!=0:
        ms.commit()
        print("commit")
#关闭连接
ms.close()

input("执行完成:")

